//
//  SecondVC.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/11/18.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
protocol SecondVCDelegate {
    func removeWindow()
}
class SecondVC: UIViewController {
    @IBOutlet weak var iv: UIImageView!
    @IBOutlet weak var btn: UIButton!
    @IBOutlet weak var bgBtn: UIButton!
    var delegate:SecondVCDelegate?
    var model: ADImageModel?
    
    var timer: NSTimer!//定时器
    
    var timeTick: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        

                    let urlStr = String(format: "%@indexads", BaseUrl)
                    DataTool.getADinfo(urlStr ,completionHandler: ({(newModel) in
                        guard newModel != nil else {
                            return
                        }
                        self.delegate?.removeWindow()
                        self.model = newModel
                        if newModel?.msg == "查询成功" {
                            
                            self.bgBtn.addTarget(self, action: #selector(self.bgBtnClicked), forControlEvents: UIControlEvents.TouchUpInside)
                            
                            self.btn.layer.masksToBounds = true
                            self.btn.layer.cornerRadius = 25
                            self.btn.setTitle("\(newModel!.timelength)秒", forState: UIControlState.Normal)
                            
                             self.iv.sd_setImageWithURL(NSURL(string: (self.model!.imgsrc)), placeholderImage: UIImage(named: "placeholder"))
                            
                            self.iv.contentMode = UIViewContentMode.ScaleAspectFill
                            self.iv.clipsToBounds = true
                            
                            self.timeTick = Int((self.model?.timelength)!)!
                            
                            self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(SecondVC.removeView), userInfo: nil, repeats: true)
        
                            
                        } else {
                             self.dismissViewControllerAnimated(false, completion: nil)
                        }
                        
        }))
        
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.timer?.fireDate = NSDate.distantPast()
        
    }
    
    override func viewDidDisappear(animated: Bool) {
        self.timer?.fireDate = NSDate.distantFuture()
    }
    
 
    func removeView() {
        self.timeTick -= 1
        
        if self.timeTick == 0 {
            
            self.dismissViewControllerAnimated(false, completion: nil)
            self.btn.setTitle("", forState: .Normal)
        } else {
            let str: String = "\(self.timeTick)"
            self.btn.setTitle("\(str)秒", forState: .Normal)
        }
        
    }
    
    func bgBtnClicked() {
        self.dismissViewControllerAnimated(false, completion: nil)
        
        let dic = ["url":self.model!.url]
        NSNotificationCenter.defaultCenter().postNotificationName("adClicked_noti", object: dic)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
